package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"staff/models"
	"strconv"
)

type StaffController struct {
	beego.Controller
}
type JsonRes struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *StaffController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "GET")
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 2)
	key := "staff-" + strconv.Itoa(page)
	val, err := models.Rdb.Get(key).Result()
	pageData := make(map[string]interface{})
	if err != nil {
		var count int64
		err = models.DB.Model(&models.Staff{}).Count(&count).Error
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "总数查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		}
		offset := (page - 1) * size
		var staff []models.Staff
		err = models.DB.Offset(offset).Limit(size).Find(&staff).Error
		pageData["count"] = count
		pageData["staff"] = staff
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			marshal, _ := json.Marshal(&pageData)
			value := string(marshal)
			models.Rdb.Set(key, value, 0)
			c.Data["json"] = JsonRes{
				Code: 200,
				Msg:  "mysql-查询成功",
				Data: pageData,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "redis-查询成功",
			Data: pageData,
		}
		c.ServeJSON()
	}

}
func (c *StaffController) Add() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "POST")
	name := c.GetString("name")
	sex := c.GetString("sex")
	department := c.GetString("department")
	tel := c.GetString("tel")
	img := c.GetString("img")
	staff := models.Staff{
		Name:       name,
		Sex:        sex,
		Department: department,
		Tel:        tel,
		Img:        img,
	}
	err := models.DB.Debug().Create(&staff).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *StaffController) Upload() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "POST")
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "请上传文件",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	path := "static/img/" + header.Filename
	c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "文件上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		imgUrl := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "文件上传成功",
			Data: imgUrl,
		}
		c.ServeJSON()
	}
}
func (c *StaffController) Del() {
	id, _ := c.GetInt("id")
	err := models.DB.Delete(&models.Staff{}, id).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
